package com.atomos.strategy.emission
{
	import flash.geom.Point;	

	/**
	 * Emits particles since and during a specific time according to a fixed generation rate.
	 * 
	 * @author Cédric Néhémie
	 */
	public class ByRateLimitedEmission extends ByRateEmission
	{
		public static const INFINITE : Number = Number.POSITIVE_INFINITY;
		
		protected var _nEmissionDuration : Number;
		protected var _nEmitSince : Number;
		protected var _nElapsedTime : Number;
		
		public function ByRateLimitedEmission( type:Class, position : Point = null, rate : Number = 1, duration : Number = 0, since : uint = 0 )
		{
			super(type, position, rate);
			
			_nEmissionDuration = checkTime( duration );
			_nEmitSince = since;
			_nElapsedTime = 0;
			
			prepareEmission( _nEmitSince );			
		}
		
		public override function prepareEmission( time : Number ) : void
		{
			_nElapsedTime += time;
			super.prepareEmission( time );
		}
		
		public override function isFinish() : Boolean
		{
			return _nElapsedTime >= _nEmissionDuration;
		}
		
		public override function hasNext() : Boolean
		{
			return super.hasNext();
		}
		
		public override function clone () : ByRateEmission
		{
			return new ByRateLimitedEmission ( _oParticleType, _pEmissionPosition, _nRate, _nEmissionDuration, _nEmitSince );
		}
		
		public function checkTime ( n : Number ) : Number
		{
			if( isNaN ( n ) || n < 0 )
				return 0;
			
			return n;
		}
		
		public function getDuration () : Number
		{
			return _nEmissionDuration;
		}
		public function getElapsed () : Number
		{
			return _nElapsedTime;
		}
	}
}